package com.smooware.icms.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.smooware.icms.R;

/**
 * Helper class, used to perform fragment navigation.
 * 
 * @author Orest Vovchak
 */
public class FragmentNavigatorHelper {

	private final FragmentActivity holder;

	public FragmentNavigatorHelper(final FragmentActivity holder) {
		this.holder = holder;
	}

	/**
	 * Finishes fragment. Received fragment is going on destroy.
	 * 
	 * @param fragment
	 *            to be finished.
	 */
	public void finishFragment(final Fragment fragment) {
		FragmentManager fragmentManager = holder.getSupportFragmentManager();

		fragmentManager.popBackStack();
	}

	/**
	 * Adds fragment to default container. Default container is specified by
	 * frame_for_fragment id.
	 * 
	 * @param fragment
	 *            to be added.
	 */
	public void addFragment(final Fragment fragment) {
		FragmentTransaction fragmentTransaction = getFragmentTransaction();

		fragmentTransaction.add(R.id.frame_for_fragment, fragment);

		fragmentTransaction.commit();
	}

	/**
	 * Replaces fragment in default container. Default container is specified by
	 * frame_for_fragment id.
	 * 
	 * @param fragment
	 *            to be shown after replacing.
	 */
	public void replaceFragment(final Fragment fragment) {
		FragmentTransaction fragmentTransaction = getFragmentTransaction();

		fragmentTransaction.replace(R.id.frame_for_fragment, fragment);

		fragmentTransaction.commit();
	}

	/**
	 * Replaces fragment in default container and adds old fragment to
	 * backstack. Default container is specified by frame_for_fragment id.
	 * 
	 * @param fragment
	 *            to be shown after replacing.
	 */
	public void replaceFragmentAndPushInBackStack(final Fragment fragment) {
		FragmentTransaction fragmentTransaction = getFragmentTransaction();

		fragmentTransaction.replace(R.id.frame_for_fragment, fragment);

		fragmentTransaction.addToBackStack(null);

		fragmentTransaction.commit();
	}

	/**
	 * Check is there any fragment in default container. Default container is
	 * specified by frame_for_fragment id.
	 * 
	 * @return true if there is no fragment, false otherwise.
	 */
	public boolean isFragmentFrameEmpty() {
		return holder.getSupportFragmentManager().findFragmentById(
				R.id.frame_for_fragment) == null;
	}

	/**
	 * Gets fragment that is in currently in default container. Default
	 * container is specified by frame_for_fragment id.
	 * 
	 * @return fragment stored in default container, null if there is none.
	 */
	public Fragment getCurrentFragment() {
		return holder.getSupportFragmentManager().findFragmentById(
				R.id.frame_for_fragment);
	}

	/**
	 * Creates fragment transaction from fragment manager.
	 * 
	 * @return created fragment transaction.
	 */
	private FragmentTransaction getFragmentTransaction() {
		FragmentManager fragmentManager = holder.getSupportFragmentManager();

		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		return fragmentTransaction;
	}
}
